package com.hainatong.shiphelper.module.warehouse.viewmodel;

import java.util.List;

import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.ChuKuRaramBean;
import com.hainatong.shiphelper.module.warehouse.entity.ChukuGoodsParam;
import com.hainatong.shiphelper.module.warehouse.entity.WorkerRecordBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class ReturnViewModel extends BaseViewModel
{

    public ReturnViewModel(@NonNull Application application)
    {
        super(application);
    }

    private MutableLiveData<LiveResult<Object>> returnResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getReturnResult()
    {
        return returnResult;
    }

    /**
     *  归还 任务
     * @param chukuGoods
     */
    public void returnTask(ChuKuRaramBean[] chukuGoods )
    {
        ApiService.Builder.getChanBoServer()
                .chuKuTask( chukuGoods )
                .compose( RxSchedulersUtils.Obs_io_main() )
                .subscribe(new VMObserver<ApiResult<Object>>()
                {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getReturnResult().postValue( new LiveResult<>( false ,errorMsg ) );
                    }

                    @Override
                    public void onSuccess(ApiResult<Object> result)
                    {
                        if( result.isOk() )
                        {
                            getReturnResult().postValue( new LiveResult<>( true , result.getData() ) );
                        }
                        else
                        {
                            getReturnResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                        }
                    }
                });
    }


    private MutableLiveData<LiveResult<Object>> employerResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getEmployerResult()
    {
        return employerResult;
    }

    /**
     *  查询 领用人 列表
     * @param name
     */
    public void queryLinYongUserListByName(String name)
    {
        ApiService.Builder.getChanBoServer()
                          .getEmployerListByName(name)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<WorkerRecordBean>>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getEmployerResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<WorkerRecordBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getEmployerResult().postValue( new LiveResult<>( true ,result.getData() ) );
                                  }
                                  else
                                  {
                                      getEmployerResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                  }
                              }
                          });
    }

    public static ChuKuRaramBean[] getReturnParams(List<ChukuGoodsParam> goodsParamsList,String chargeUser,String chargeUserId)
    {
        ChuKuRaramBean[] chuKuBeans = new ChuKuRaramBean[ goodsParamsList.size() ];

        for( int i=0 ; i < goodsParamsList.size() ;i++ )
        {
            // 归还 商品
            ChuKuRaramBean chuKuGoods = ChuKuRaramBean.getReturnParamsByGoodsInfo( goodsParamsList.get(i) );
            chuKuGoods.setCharge_user(chargeUser);
            chuKuGoods.setCharge_user_id( chargeUserId );
            chuKuBeans[i] = chuKuGoods;
        }

        return chuKuBeans;
    }

}
